package com.wenwei.sharebox.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.base.Constant;
import com.wenwei.sharebox.bean.UserIdentityBean;
import com.wenwei.sharebox.bean.UserLoginBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.RogerSPUtils;
import com.wenwei.sharebox.utils.UserStringUtils;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

/**
 * Created by Administrator on 2017/7/13.
 */
public class AuthenticationActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.input_name)
    EditText inputName;
    @Bind(R.id.input_user_card)
    EditText inputUserCard;
    @Bind(R.id.commit_btn)
    TextView commitBtn;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_authentication;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        super.initView();

        baseTitle.setText("实名认证");

        Observable<CharSequence> nameObservable = RxTextView.textChanges(inputName);
        Observable<CharSequence> phoneObservable = RxTextView.textChanges(inputUserCard);

        Observable.combineLatest(nameObservable, phoneObservable, new Func2<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean call(CharSequence charSequence, CharSequence charSequence2) {
                return !charSequence.toString().equals("") && !charSequence2.toString().equals("");
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                commitBtn.setEnabled(aBoolean);
                if(aBoolean){
                    commitBtn.setBackgroundResource(R.drawable.bc_share_select);
                }else{
                    commitBtn.setBackgroundResource(R.drawable.bc_click_false);
                }
            }
        });
    }

    @OnClick({R.id.base_back, R.id.commit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.commit_btn:
                userIdentity();
                break;
        }
    }

    BaseSubscriber<UserIdentityBean> identitySubscriber;
    private void userIdentity(){
        identitySubscriber = new BaseSubscriber<UserIdentityBean>(mContext) {
            @Override
            public void onSuccess(UserIdentityBean userIdentityBean) {
                showToast("认证成功");
                UserLoginBean ub = RogerSPUtils.getObject(mContext, Constant.SP_USER_LOGIN_RESULT,UserLoginBean.class);
                ub.setStatus(1);
                RogerSPUtils.putObject(mContext, Constant.SP_USER_LOGIN_RESULT,ub);
                finish();
            }
        };
        try{
            JSONObject jb = new JSONObject();
            jb.put("idCard",inputUserCard.getText().toString());
            jb.put("realName",inputName.getText().toString());
            ApiManager.getInstance().userIdentity(identitySubscriber,inputUserCard.getText().toString(),inputName.getText().toString(), UserStringUtils.getUserId(mContext));
        }catch(Exception e){}
    }
}
